﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PhotoMgr.src.common;
using System.Windows.Forms;
using PhotoMgr.src.database;

namespace PhotoMgr.src.gui.Image
{
    class ImageList : ListBase
    {
        PictureBox pbx_image;
        public List<View_ImageDetail> SelectedList { get; set; }
        Button btn_selectall;

        public ImageList(string title, FormAction action)
            : base(title, action)
        {
            DataColumns = new DataGridViewColumn[] 
            { 
                Global.GenColumn("Tên hình"           , "ImageTitle").SetSizeMode(DataGridViewAutoSizeColumnMode.Fill),
                Global.GenColumn("Người dự thi"            , "ArtistName"),
                Global.GenColumn("Thể loại"                , "CategoryName"),
                //Global.GenColumn("Tình trạng"               , "Status"),
            };

            SearchControls = new SearchBox[]{                
                new SearchBox().initTextBox("Tên hình ảnh=ImageTitle"),
                new SearchBox().initComboBox("Thể loại ảnh=CategoryID", new SearchBox.FillComboBoxDelegate(DBManager.Fill_Category), true),
                new SearchBox().initComboBox("Người dự thi=ArtistID", new SearchBox.FillComboBoxDelegate(DBManager.Fill_Artist), true),
                //new SearchBox().initComboBox("Quyền hạn=RoleCode", new SearchBox.InitStatusComboBoxDelegate(Global.InitStatusCombobox), Global.StatusType.RoleCode, true ),
                //new SearchBox().initComboBox("Tình trạng=Status", new SearchBox.InitStatusComboBoxDelegate(Global.InitStatusCombobox), Global.StatusType.Status, true ),
            };

            OwnerSearchBoxIndex = -1;
            SelectedColumnName = "ImageID";
            SearchFunction = DBManager.Search_Image;
            //DetailFormType = typeof(eHospital.source.gui.hanhchanh.NhanVien_Detail1);

            pbx_image = new PictureBox();
            pbx_image.SizeMode = PictureBoxSizeMode.Zoom;
            pbx_image.BorderStyle = BorderStyle.Fixed3D;
            this.Controls.Add(pbx_image);

            dgv_list.Size = new System.Drawing.Size(491, dgv_list.Height);            
            pbx_image.Size = new System.Drawing.Size(265, dgv_list.Height);
            pbx_image.Location = new System.Drawing.Point(512, dgv_list.Location.Y);

            dgv_list.LocationChanged += new EventHandler(dgv_list_SizeChanged);
            dgv_list.SizeChanged += new EventHandler(dgv_list_SizeChanged);

            dgv_list.CellClick += new DataGridViewCellEventHandler(dgv_list_CellClick);
            dgv_list.MultiSelect = true;
            dgv_list.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            SelectedList = new List<View_ImageDetail>();
        }

        void dgv_list_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            View_ImageDetail image = (View_ImageDetail)DataSource[e.RowIndex];
            pbx_image.Image = System.Drawing.Image.FromFile(image.ImagePath);
            pbx_image.SizeMode = PictureBoxSizeMode.Zoom;

            GC.Collect();
        }

        void dgv_list_SizeChanged(object sender, EventArgs e)
        {
            dgv_list.Size = new System.Drawing.Size(491, dgv_list.Height);
            pbx_image.Size = new System.Drawing.Size(this.Width - 40 - 491, dgv_list.Height);
            pbx_image.Location = new System.Drawing.Point(512, dgv_list.Location.Y);
        }

        public override void ButtonSelect()
        {
            if (updateSelectedItem())
            {
                SelectedList.Clear();

                foreach (DataGridViewRow item in dgv_list.SelectedRows)
                {
                    SelectedList.Add((View_ImageDetail)DataSource[item.Index]);
                }

                this.Close();
            }
        }
    }
}
